﻿using HenuAlbum.DbContext;
using HenuAlbum.Identity;
using HenuAlbum.Models;
using HenuAlbum.ResponseModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HenuAlbum.Controllers
{
    [ApiController]
    [Route("api/[controller]")]

    public class HotRecommendController : ControllerBase
    {
        private readonly UserManager<User> _userManager;
        private readonly AppDbContext _appDbContext;

        public HotRecommendController(UserManager<User> userManager, AppDbContext appDbContext)
        {
            _userManager = userManager;
            _appDbContext = appDbContext;
        }

        [HttpPost("{id:int}/Delete")]
        [Authorize(Roles = "管理员")]
        public async Task<IActionResult> Delete(int id)
        {

            var qhot = await _appDbContext.HotRecommend.Where(x => x.Id == id).FirstOrDefaultAsync();
            if (null == qhot) return BadRequest(new NotExistsError("该推荐不存在"));
            _appDbContext.HotRecommend.Remove(qhot);
            await _appDbContext.SaveChangesAsync();
            return Ok(new OkResponse());
        }

        [HttpPost("{albumid:int}/Add")]
        [Authorize(Roles = "管理员")]
        public async Task<IActionResult> Add(int albumid)
        {
            //获取身份信息
            var user = await _userManager.GetUserAsync(this.User);
            //获取相册信息，判断是否存在
            var album = await _appDbContext.Albums.Where(x => x.Id == albumid).FirstOrDefaultAsync();
            if (null == album) return BadRequest(new NotExistsError("相册不存在"));
            HotRecommend hotRecommend = new HotRecommend()
            {
                Creator = user,
                CreateTime = System.DateTime.Now,
                AlbumId = albumid,
                CreatorId = user.Id
            };
            await _appDbContext.HotRecommend.AddAsync(hotRecommend);
            await _appDbContext.SaveChangesAsync();
            return Ok(new OkResponse()
            {
                Data = new
                {
                    Id = hotRecommend.Id,
                    Albumid = hotRecommend.AlbumId,
                    CreaterTime = hotRecommend.CreateTime,
                    CreatorName = user.UserName
                }
            });
        }


        [HttpGet("Get")]
        public async Task<IActionResult> Get()
        {
            var result = await (from hot in _appDbContext.HotRecommend
                          let coverId = _appDbContext.Resources
                          .Where(x => x.AlbumId == hot.AlbumId && !x.IsLocked && x.Type == ResourceType.Picture)
                          .Select(x => x.Id)
                          .FirstOrDefault()
                          select new
                          {
                              hot.Id,
                              hot.AlbumId,
                              AlbumTitle = hot.Album.Title,
                              AlbumDescription = hot.Album.Description,
                              AlbumType = hot.Album.Discriminator,
                              CoverId = coverId
                          }).ToListAsync();

            return Ok(new ArrayResponse(result));
        }

    }
}
